package com.atguigu.gulimall.service.impl;

import com.atguigu.gulimall.service.PdfService;
import com.atguigu.gulimall.utils.IndonesiaHeaderFooter;
import com.atguigu.gulimall.utils.PdfFilePathUtil;
import com.atguigu.gulimall.utils.PdfTemplateUtils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * developer: 肖凯旋
 * time: 28/5/2024
 */
@Service
public class PdfServiceImpl implements PdfService {
    @Override
    public void dowload() {

        NewPdf newPdf = new NewPdf();
        String file = newPdf.file(UUID.randomUUID().toString());
        System.out.println(file);
    }
}


@Slf4j
class NewPdf extends PdfFilePathUtil {

    @Override
    public String file(String nameSuffix) {
        String fileName = "HealthWarn" + "_" + nameSuffix + ".pdf";
        String path = "D:/data" + "/" + fileName;
        log.info("此次生成的PDF文档的路径为為：" + path);

        //创建文档对象, 并写进指定路径的文件
        Document document = new Document(PageSize.A4, 10, 10, 30, 70);
        try {
            File file = new File(path);
//            if (file.createNewFile()){
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
            SimpleDateFormat time = new SimpleDateFormat("yyyyMM");
            String format = time.format(new Date());
            //pdfWriter.setPageEvent(new MyHeaderFooter(PDFTemplateUtils.bfmsjhChinese, PDFTemplateUtils.bfEnglish, "", logoFilePath));
            BaseFont bfmsjhChinese = BaseFont.createFont("/fonts/SimHei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            BaseFont bfEnglish = BaseFont.createFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            pdfWriter.setPageEvent(new IndonesiaHeaderFooter(bfmsjhChinese, bfEnglish,
                    "ARL International Limited\n" +
                            "Labuan F.T., Malaysia \n" +
                            "Co. No. LL04581 License No. BS200546",
                    8, ""));
            document.open();
            //这里为文档对象添加内容
//            document.add(new Paragraph("Hello！ PDF！！！"));  //测试
            createPDf(document);
        } catch (Exception e) {
            log.error("error", e);
        } finally {
            document.close();
        }
        return path;
    }


    private void createPDf(Document document) throws DocumentException {

        log.info("开始生成pdf");

        Phrase phrase = null;
        PdfPCell pdfPCell = null;

        PdfPTable pdfPTable = createTable(new float[]{280, 720});
        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_BOLD_12f);
        phrase.add("");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
        pdfPTable.addCell(pdfPCell);
        //2
        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_BOLD_12f);
        phrase.add("Foshan Shunde Topcod Industry CO., LTD");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
        pdfPCell.setRowspan(2);
        pdfPTable.addCell(pdfPCell);
        document.add(pdfPTable);


        pdfPTable = createTable(new float[]{260, 740});
        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_normal_7f);
        phrase.add("");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
        pdfPCell.setPadding(1);
        pdfPTable.addCell(pdfPCell);

        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_normal_7f);
        phrase.add("Adds: No.10, 1st Road, Jibei Industrial Zone, Longjiang, Shunde, Foshan, China");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
        pdfPCell.setPadding(1);
        pdfPTable.addCell(pdfPCell);
        document.add(pdfPTable);


        pdfPTable = createTable(new float[]{320, 680});
        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_normal_6f);
        phrase.add("");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
        pdfPCell.setPadding(-8);
        pdfPTable.addCell(pdfPCell);

        phrase = new Phrase();
        phrase.setFont(PdfTemplateUtils.english_arial_normal_6f);
        phrase.add("ATTN: Ming Cheung Email: tp05@topcod.com.cn Mobile: +86 13824524163");
        pdfPCell = PdfTemplateUtils.createCell(phrase, Element.ALIGN_LEFT, 0);
        pdfPCell.setBorder(0);
//        pdfPCell.setFixedHeight(10);
        pdfPCell.setPadding(-8);
        pdfPTable.addCell(pdfPCell);
        document.add(pdfPTable);




    }

    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(500);
            table.setLockedWidth(true);
            table.setPaddingTop(1);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            log.error("error", e);
        }
        return table;
    }

}
